package controllers;

import java.util.List;
import java.util.Map;

import models.my.MyPosts;
import models.user.UserInfo;
import play.Logger;
import play.data.DynamicForm;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import utils.MengException;
import views.html.*;


public class UserPosts extends Controller{

	public static Result createPosts(){
		
		Map<String,String> map = DynamicForm.form().bindFromRequest().data();
		Logger.info(Json.toJson(map).toString());
		
		MyPosts posts = MyPosts.getInstances();
		posts.username = session().get("username");
		posts.content = map.get("content");
		
		try {
			boolean flag = posts.createPosts(posts);
			if(flag){
				return redirect("/");
			}
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ok("create Posts failure");
	}
	
	public static Result getPosts(int page, int size){
		
		String username  = session().get("username");
		
		try {
			List<MyPosts> posts = MyPosts.getInstances().getPostsPage(username, page, size);
			List<UserInfo> uis = UserInfo.getInstances().getUserInfo(0, 10);
			return ok(index.render(posts,uis));
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ok("get Posts failure");
	}
}
